package tanxin;

import java.util.Arrays;

public class leetCode135_candy {
    public int candy(int[] ratings) {
        if(ratings == null || ratings.length == 0) return 0;
        int[] cost = new int[ratings.length];
        Arrays.fill(cost,1);
        int result = 0;
        for (int i = 1; i < ratings.length; i++) {
            if(ratings[i]>ratings[i - 1]){
                cost[i] = cost[i - 1] + 1;
            }
        }

        for (int i = ratings.length - 2; i >= 0; i--) {
            if(ratings[i] > ratings[i + 1]){
                // 因为之前比较过一次右边比左边大，如果一个人既大于左边也大于右边，就要取最大值
                cost[i] = Math.max(cost[i + 1] + 1,cost[i]);
            }
        }
        for (int i : cost) {
            result += i;
        }
        return result;

    }


}
